package com.axc.gallery2.net.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.axc.gallery2.R;
import com.renren.api.connect.android.Renren;

/**
 * 网络相册根布局
 *
 */
public abstract class BaseActivity extends Activity {
	/**
	 * 根布局对象
	 */
	protected LinearLayout root;

	/**
	 * 标题栏左边按钮
	 */
	protected Button titlebarLeftButton;

	/**
	 * 标题栏中间文字
	 */
	protected TextView titlebarText;

	/**
	 * 标题栏右边按钮
	 */
	protected Button titlebarRightButton;

	/**
	 * 调用SDK接口的Renren对象
	 */
	protected Renren renren;
	/**
	 * 对话框
	 */
	private AlertDialog alertDialog = null;
	private ProgressDialog progressDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		/*
		 * // 设置不显示标题栏 requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置布局
		 * setContentView(R.layout.net_root_layout); // 初始化控件 root =
		 * (LinearLayout) findViewById(R.id.net_root);
		 */

		// 设置不显示标题栏
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		// 设置布局
		setContentView(R.layout.net_root_layout);
		// 初始化控件
		root = (LinearLayout) findViewById(R.id.net_root);
		titlebarLeftButton = (Button) findViewById(R.id.net_titlebar_left_button);
		titlebarRightButton = (Button) findViewById(R.id.net_titlebar_right_button);
		titlebarText = (TextView) findViewById(R.id.net_titlebar_title_text);

		// 注册“更多”按钮事件
		titlebarRightButton.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				showDialog(0);
			}
		});

		// 注册“返回”按钮事件(子类若有不同的处理，则需覆盖此事件)
		titlebarLeftButton.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});

		// 注册事件

		Intent intent = getIntent();
		renren = intent.getParcelableExtra(Renren.RENREN_LABEL);
		if (renren != null) {
			renren.init(this);
		}
	}

	/**
	 * 解析逗号分割的字符串
	 * 
	 * @return
	 */
	protected String[] parseCommaIds(String s) {
		if (s == null) {
			return null;
		}
		String[] ids = s.split(",");
		return ids;
	}

	/**
	 * 显示等待框
	 */
	protected void showProgress() {
		showProgress("Please wait", "progressing");
	}

	/**
	 * 显示等待框
	 * 
	 * @param title
	 * @param message
	 */
	protected void showProgress(String title, String message) {
		progressDialog = ProgressDialog.show(this, title, message);
	}

	/**
	 * 取消等待框
	 */
	protected void dismissProgress() {
		if (progressDialog != null) {
			try {
				progressDialog.dismiss();
			} catch (Exception e) {

			}
		}
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		if(alertDialog==null){
			ListView listView =new ListView(BaseActivity.this);
			listView.setAdapter(new DListAdapter(BaseActivity.this));
			listView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1,
						int position, long arg3) {
					switch (position) {
					//刷新
					case 0:
						
						break;
				    //注销
					case 1:
						LoadOut();
						break;
					//关闭
					default:
						break;
					}
					alertDialog.dismiss();
										
				}
			});
			alertDialog =new AlertDialog.Builder(BaseActivity.this).setView(listView).create();
		}
		return alertDialog;
	}
	/**
	 * 登出
	 */
	protected abstract void LoadOut();
	/**
	 * 显示Toast提示
	 * 
	 * @param message
	 */
	protected void showTip(String message) {
		Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		// 不销毁ProgressDialog会出现view not attached to window manager异常
		dismissProgress();
	}

	/**
	 * 对话框适配器
	 * 
	 */

	class DListAdapter extends BaseAdapter {
		private Context context;

		public DListAdapter(Context context) {
			this.context = context;
			list.add("刷新");
			list.add("注销");
			list.add("关闭");
		}

		List<String> list = new ArrayList<String>();

		public List<String> getList() {
			return list;
		}

		public void setList(List<String> list) {
			this.list = list;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View arg1, ViewGroup arg2) {
			TextView textView =new TextView(context);
			textView.setHeight(60);
			textView.setPadding(0, 5, 0, 5);
			textView.setGravity(Gravity.CENTER);
			textView.setText(list.get(position));
			return textView;
		}
	}

}
